#include "helper/sqlite/Error.h"

#include <sqlite3.h>
#include <sstream>

namespace helper {
namespace sqlite {
namespace error  {

const char * SqliteErrorCategory::name() const noexcept {
  return "SqliteErrorCategory";
}
std::string SqliteErrorCategory::message(int ev) const {
  return std::string(sqlite3_errstr(ev));
}
std::error_condition SqliteErrorCategory::default_error_condition(int ev) const noexcept {
  switch(sqlite_error(ev)){
    //case sqlite::ok :
      //return std::errc::success;
    case sqlite::error::perm  : 
      return std::errc::permission_denied;
    case sqlite::error::nomem:
      return std::errc::not_enough_memory;
    case sqlite::error::interrupt:
      return std::errc::interrupted;
    case sqlite::error::ioerr: 
      return std::errc::io_error;
    case sqlite::error::range:
      return std::errc::result_out_of_range;
    default:
      return std::error_condition(ev, *this);
  }
}

}//namespace error
}//namespace sqlite
}//namespace helper

#include <iostream> //TODO:  use the log , think more and more 
namespace helper {
namespace sqlite {
namespace error {

std::string FormatErrorMessage(const char * func, const int line, std::error_code & err) {
  std::ostringstream oss;
  oss << "[" << func << ":" << line << "]"
      << "[" << err.value() << ":" << err.message() <<"]"
      ;
  return oss.str();
}

void ShowErrorMsg(const char * func, const int line, std::error_code & err){
  std::cerr << FormatErrorMessage(func, line, err) << std::endl;
}


}//namespace error
}//namespace sqlite 
}//namespace helper 
